<?php
$title = '操作审计';
/** @var array $logs */
/** @var array $query */
require __DIR__ . '/../partials/head.php';
require __DIR__ . '/../partials/nav.php';
?>
<section class="section">
    <div class="app-container" style="display:flex;flex-direction:column;gap:24px;">
        <div class="card" style="gap:18px;">
            <h2 style="margin:0;">审计检索</h2>
            <form method="get" action="/admin/audit" style="display:grid;gap:16px;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));">
                <div class="field">
                    <label>起始时间（ISO）</label>
                    <input type="text" name="from" value="<?= htmlspecialchars($query['from'] ?? '') ?>">
                </div>
                <div class="field">
                    <label>截止时间（ISO）</label>
                    <input type="text" name="to" value="<?= htmlspecialchars($query['to'] ?? '') ?>">
                </div>
                <div class="field">
                    <label>脚本</label>
                    <input type="text" name="script" value="<?= htmlspecialchars($query['script'] ?? '') ?>">
                </div>
                <div class="field">
                    <label>用户 ID</label>
                    <input type="text" name="user" value="<?= htmlspecialchars($query['user'] ?? '') ?>">
                </div>
                <div class="field">
                    <label>Action</label>
                    <input type="text" name="status" value="<?= htmlspecialchars($query['status'] ?? '') ?>">
                </div>
                <div class="field">
                    <label>条数</label>
                    <input type="number" name="limit" value="<?= htmlspecialchars((string)($query['limit'] ?? 100)) ?>" min="1" max="1000">
                </div>
                <div style="grid-column:1 / -1;display:flex;gap:12px;justify-content:flex-end;">
                    <button class="btn-ghost btn" type="submit">检索</button>
                    <a class="btn" href="/admin/audit?format=csv">导出 CSV</a>
                </div>
            </form>
        </div>
        <div class="card" style="gap:16px;">
            <h3 style="margin:0;">审计结果（<?= count($logs['items']) ?>）</h3>
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>用户</th>
                    <th>Action</th>
                    <th>目标</th>
                    <th>时间</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($logs['items'] as $log): ?>
                    <tr>
                        <td><?= htmlspecialchars((string)$log['id']) ?></td>
                        <td><?= htmlspecialchars((string)($log['actor_id'] ?? '-')) ?></td>
                        <td><?= htmlspecialchars($log['action']) ?></td>
                        <td><?= htmlspecialchars($log['target']) ?></td>
                        <td><?= \Frontend\Helpers\Format::datetime($log['created_at'] ?? '') ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>
<?php require __DIR__ . '/../partials/footer.php'; ?>
